//
//  LGTPostUrlStepVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/23.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import WisdomHUD

//0复制数据  1输入网址
class LGTPostUrlStepVC: LGTBaseVC {

    var textView:LGTInputTextView?
    var textView2:LGTInputTextView?
    var type:Int = 0  //0复制数据  1输入网址
    var index:Int = 0 //在视图上的索引位置
    var clickInfoBlock:ClickInfoBlock? //保存回掉
    var path:IndexPath?
    private var _info:Dictionary<String, Any>?
    //初始化信息
    var info:Dictionary<String, Any>?{
        get{
            return _info
        }set{
            _info = newValue
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setUI() {
        super.setUI()
        self.view.backgroundColor = BlackAlphaColor
        let backView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(10))
        self.view.addSubview(backView)
        backView.isUserInteractionEnabled = true
        backView.backgroundColor = UIColor.white
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(self.view)
            make.width.equalTo(SizeFit(303))
            make.height.equalTo(SizeFit(401))
        }
        
        let data:Array<Dictionary<String, Any>> = [["title":"复制数据",
                                                   "subtitle":"填写数据*",
                                                   "place1":"适用于需要复制数据，如填写邀请码，输 入内容，提示会员复制数据，方便填写",
                                                   "place2":"如邀请码，钱包地址之类"],
                                                   ["title":"输入网址",
                                                    "subtitle":"输入网址*",
                                                    "place1":"适用于需要扫描二维码的操作，输入内容 详细描述如何扫描二维码。",
                                                    "place2":"链接"]]
        
        let info:Dictionary<String, Any> = data[self.type]
        let title:String = info["title"] as! String
        let subtitle:String = info["subtitle"] as! String
        let place1:String = info["place1"] as! String
        let place2:String = info["place2"] as! String
        
        let titleLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: 0, text: title)
        backView.addSubview(titleLabel)
        
        titleLabel.font = UIFont.boldSystemFont(ofSize: SizeFitFloat(18))
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(backView)
            make.height.equalTo(SizeFit(18))
            make.top.equalTo(SizeFit(22))
        }
        
        let clostBtn = LGTViewCreateUtil.share.createLGTButton(image: "close_icon", selected: "close_icon", dW: SizeFitFloat(10), dH: SizeFitFloat(10))
        backView.addSubview(clostBtn)
        clostBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(SizeFit(13))
            make.top.equalTo(SizeFit(23))
            make.right.equalTo(-SizeFit(24))
        }
        
        let descLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "步骤说明*")
        backView.addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(27))
            make.top.equalTo(SizeFit(59))
            make.height.equalTo(SizeFitFloat(15))
        }
        BaseUtil.setLabelSpecial(label: descLabel, str: "步骤说明*", specialStr: "*", normalAttr: [NSAttributedString.Key.foregroundColor:TextColor], specialAttr: [NSAttributedString.Key.foregroundColor:ColorUtil(color: 0xFF0101)
            ])
        
        let imageView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(3))
        imageView.isUserInteractionEnabled = true
        imageView.backgroundColor = ViewBackColor
        backView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(backView)
            make.height.equalTo(SizeFitFloat(102))
            make.width.equalTo(SizeFitFloat(252))
            make.top.equalTo(SizeFit(84))
        }
        
        self.textView = LGTViewCreateUtil.share.createInputTextView(placehoder: place1, fontSize: SizeFitFloat(12), limitCout: 200)
        imageView.addSubview(self.textView!)
        self.textView?.textView.backgroundColor = UIColor.clear
        self.textView?.textView.isScrollEnabled = true
        self.textView?.showCount = true
        self.textView?.backgroundColor = UIColor.clear
        self.textView!.snp.makeConstraints({ (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(SizeFit(10))
            make.width.equalTo(SizeFitFloat(230))
            make.bottom.equalTo(SizeFit(-18))
        })
        
        let titleLabel2:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: subtitle)
        backView.addSubview(titleLabel2)
        //titleLabel2.font = UIFont.boldSystemFont(ofSize: SizeFitFloat(18))
        titleLabel2.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(27))
            make.height.equalTo(SizeFit(15))
            make.top.equalTo(SizeFit(205))
        }
        BaseUtil.setLabelSpecial(label: titleLabel2, str: subtitle, specialStr: "*", normalAttr: [NSAttributedString.Key.foregroundColor:TextColor], specialAttr: [NSAttributedString.Key.foregroundColor:ColorUtil(color: 0xFF0101)
            ])
        
        
        let imageView2:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(3))
        imageView2.isUserInteractionEnabled = true
        imageView2.backgroundColor = ViewBackColor
        backView.addSubview(imageView2)
        imageView2.snp.makeConstraints { (make) in
            make.centerX.equalTo(backView)
            make.height.equalTo(SizeFitFloat(37))
            make.width.equalTo(SizeFitFloat(252))
            make.top.equalTo(SizeFit(231))
        }
        
        
        self.textView2 = LGTViewCreateUtil.share.createInputTextView(placehoder: place2, fontSize: SizeFitFloat(12), limitCout: -1)
        imageView2.addSubview(self.textView2!)
        self.textView2?.textView.backgroundColor = UIColor.clear
        self.textView2?.textView.isScrollEnabled = true
        self.textView2?.showCount = true
        self.textView2?.backgroundColor = UIColor.clear
        self.textView2!.snp.makeConstraints({ (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(SizeFit(13))
            make.width.equalTo(SizeFitFloat(230))
            make.height.equalTo(SizeFit(20))
        })
        
        
        let sureButton:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "确认添加", font: Font_Normal_15, color: UIColor.white, selectedColor: UIColor.white, corner:SizeFitFloat(20), backColor: YellowColor)
        backView.addSubview(sureButton)
        sureButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(SizeFit(40))
            make.width.equalTo(SizeFit(252))
            make.top.equalTo(SizeFit(292))
        }
        
        let cancelBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "取消", font: Font_Normal_15, color: YellowColor, selectedColor: YellowColor, corner:SizeFitFloat(20), backColor: UIColor.clear)
        backView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(SizeFit(40))
            make.width.equalTo(SizeFit(252))
            make.top.equalTo(sureButton.snp_bottomMargin).offset(SizeFit(10))
        }
        
        cancelBtn.addTarget(self, action: #selector(closeAction), for: UIControl.Event.touchUpInside)
        clostBtn.addTarget(self, action: #selector(closeAction), for: UIControl.Event.touchUpInside)
        sureButton.addTarget(self, action: #selector(sureAction), for: UIControl.Event.touchUpInside)
        
        if self.info != nil {
            self.textView?.text = self.info?["stepText"] as? String ?? ""
            self.textView2?.text = self.info?["subText"] as? String ?? ""
        }
    }
    
    @objc func addImageAction(){
        print("addImage")
    }
    
    @objc func closeAction(){
        print("closeAction")
        self.dismiss(animated: false, completion: nil)
    }
    
    @objc func sureAction(){
        print("sureAction")
        if self.textView?.textView.text.count == 0{
            WisdomHUD.showText(text: "请输入步骤说明")
            return
        }
        
        
        
        // var type:Int = 0  //0复制数据  1输入网址
        let data:Array<String> = ["请填写数据", "请输入网址"]
        
        if self.textView2?.textView.text.count == 0{
            WisdomHUD.showText(text: data[self.type])
            return;
        }
        
      
//        //判断网址格式
//        if self.type == 1{
//            WisdomHUD.showText(text: "填写的网络格式有误")
//        }
        
        let copyState:Bool = self.type == 0 ? true:false
        
        
        var info:Dictionary<String, Any> = ["index": self.index,
                                            "stepText": self.textView!.textView.text,
                                            "subText": self.textView2!.textView.text,
                                            "isurl": !copyState, //输入网址
                                            "copy": copyState,
                                            "collection":copyState,  //复制数据
                                            ]
        
        let pathIndexPath:IndexPath? = info["path"] as? IndexPath
        if path != nil{
            info["path"] = pathIndexPath!
        }
        
//        if self.path != nil{
//            info["path"] = self.path!
//        }
        
        if self.clickInfoBlock != nil{
            self.clickInfoBlock!(self.index, info)
        }
        self.dismiss(animated: false, completion: nil)
    }
    
}
